<?php
namespace App\SearchBuilders;

class ProductSearchBuilder{

    protected $params = [
        'index' => 'products',
        'type'  => '_doc',
        'body'  => [
            'query' => [
                'bool' => [
                    'filter' => [],
                    'must'   => [],
                ],
            ],
        ],
    ];

    // 添加分页查询
    public function paginate($page=1,$perPage=10)
    {
        $this->params['body']['from'] = ($page - 1) * $perPage;
        $this->params['body']['size'] = $perPage;
        return $this;
    }

    public function orderBy($order="created_at",$sort="desc"){
        if (!isset($this->params['body']['sort'])) {
            $this->params['body']['sort'] = [];
        }
        $this->params['body']['sort'][] = [$order => ['order'=>$sort]];
        return $this;
    }

    // 添加搜索词
    public function keywords($keywords)
    {
        // 如果参数不是数组则转为数组
        $keywords = is_array($keywords) ? $keywords : [$keywords];
        foreach ($keywords as $keyword) {
            $this->params['body']['query']['bool']['must'][] = [
                'multi_match' => [
                    'query'  => $keyword,
                    'fields' => [
                        'user_name^2',
                        'consignee^2',
                        'store_name^2',
                        'addresses',
                        'goodsName',
                        'goodsSpec',
                        'properties_value'
                    ],
                ],
            ];
        }
        $this->params['body']['aggs'] = [
            'properties' => [
                'nested' => [
                    'path' => 'properties',
                ],
                'aggs'   => [
                    'properties' => [
                        'terms' => [
                            'field' => 'properties.name',
                        ],
                        // 第三层聚合
                        'aggs'  => [
                            // 聚合的名称
                            'value' => [
                                'terms' => [
                                    'field' => 'properties.value',
                                ],
                            ],
                        ],
                    ],
                ],
            ]
        ];
        return $this;
    }

    // 添加一个按商品属性筛选的条件
    public function propertyFilter($name, $value,$type="filter")
    {
        $this->params['body']['query']['bool'][$type][] = [
            'nested' => [
                'path'  => 'properties',
                'query' => [
                    ['term' => ['properties.search_value' => $name.':'.$value]],
                ],
            ],
        ];
        return $this;
    }

    //获取相似商品的id
    public function getSimilarProductIds($product,$perPage=10){
        $this->paginate(1,$perPage);
        foreach ($product->properties as $property) {
            $this->propertyFilter($property->name,$property->value,'should');
        }
        $this->params['body']['query']['bool']['minimum_should_match'] = ceil(count($product->properties) / 2);
        $this->params['body']['query']['bool']['must_not'] = [['term' => ['_id' => $product->id]]];
        $result = app('es')->search($this->getParams());
        return collect($result['hits']['hits'])->pluck('_id')->all();
    }

    // 返回构造好的查询参数
    public function getParams()
    {
        return $this->params;
    }
}
